﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Converters;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using 自定义控件.Components;

namespace 自定义控件
{
    /// <summary>
    /// CopyWindow.xaml 的交互逻辑
    /// </summary>
    public partial class CopyWindow : Window
    {
        public CopyWindow()
        {
            InitializeComponent();
        }

        private void UserControl_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // 获取控件（例如 UserControl1）
            var userControl = sender as UserControl;

            // 创建拖拽数据，可以是控件的某种标识
            DataObject dragData = new DataObject("UserControl", userControl);

            // 开始拖拽
            DragDrop.DoDragDrop(userControl, dragData, DragDropEffects.Copy);
        }

        private void Canvas_DragOver(object sender, DragEventArgs e)
        {
            // 必须设置拖拽效果才能正常拖拽
            if (e.Data.GetDataPresent("UserControl"))
            {
                e.Effects = DragDropEffects.Copy;
            }
            else
            {
                e.Effects = DragDropEffects.None;
            }
        }

        private void Canvas_Drop(object sender, DragEventArgs e)
        {
            // 获取拖拽的数据
            var data = e.Data.GetData("UserControl") as UserControl;
            if (data != null)
            {
                // 克隆控件（可以是通过 XAML 重建或者直接通过代码克隆）
                var clonedControl = CloneControl(data);
                clonedControl.Width = 200;
                clonedControl.Height = 80;
                clonedControl.MouseLeftButtonDown += ClonedControl_MouseLeftButtonDown;
                clonedControl.MouseMove += ClonedControl_MouseMove;
                clonedControl.MouseLeftButtonUp += ClonedControl_MouseLeftButtonUp;

                // 设置控件在 Canvas 上的位置
                var position = e.GetPosition(canvas);  // 获取鼠标在 Canvas 上的点击位置
                Canvas.SetLeft(clonedControl, position.X);
                Canvas.SetTop(clonedControl, position.Y);

                // 将控件添加到 Canvas 上
                canvas.Children.Add(clonedControl);
            }
        }

        bool isMoving = false;
        Point sp;
        //鼠标按下控件的位置
        Point _mouseDownControlPosition;
        //int zIndex = 10;
        private void ClonedControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            UserControl? c = sender as UserControl;
            isMoving = true;
            sp = e.GetPosition(this);
            _mouseDownControlPosition = new Point(double.IsNaN(Canvas.GetLeft(c)) ? 0 : Canvas.GetLeft(c), double.IsNaN(Canvas.GetTop(c)) ? 0 : Canvas.GetTop(c));

            //zIndex = Panel.GetZIndex(c);
            foreach (UIElement elem in canvas.Children)
            {
                int zIndex = Panel.GetZIndex(elem);
                if ((zIndex > 0))
                {
                    Panel.SetZIndex(elem,zIndex -1);
                }
            }
            Panel.SetZIndex(c,1000);
            c.CaptureMouse();
        }

        private void ClonedControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMoving)
            {
                UserControl? c = sender as UserControl;
                Point point = e.GetPosition(this);

                var dp = point - sp;
                Canvas.SetLeft(c, _mouseDownControlPosition.X + dp.X);
                Canvas.SetTop(c, _mouseDownControlPosition.Y + dp.Y);
            }
        }  

        private void ClonedControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            UserControl? c = sender as UserControl;
            isMoving = false;
            c.ReleaseMouseCapture();
        }


        private UserControl CloneControl(UserControl original)
        {
            // 在这里实现控件克隆的逻辑，可能是通过复制控件的元素树，或者简单地创建一个新的控件
            if (original is UserControl1)
            {
                return new UserControl1();
            }
            else if (original is UserControl2)
            {
                return new UserControl2();
            }

            return null;
        }
    }
}
